package com.example.othree.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.othree.entity.WheatCornGrowthYear;
import com.example.othree.mapper.WheatCornGrowthYearMapper;
import com.example.othree.service.WheatCornGrowthYearService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zogran
 * @since 2024-11-12
 */
@Service
public class WheatCornGrowthYearServiceImpl extends ServiceImpl<WheatCornGrowthYearMapper, WheatCornGrowthYear> implements WheatCornGrowthYearService {
    @Resource
    WheatCornGrowthYearMapper wheatCornGrowthYearMapper;
    @Override
    public IPage PageList(IPage<WheatCornGrowthYear> page, Wrapper wrapper) {
        return wheatCornGrowthYearMapper.PageList(page,wrapper);
    }

    @Override
    public String[] getTimeRange(String stationName, String year, String crop, String startPeriod, String endPeriod) {
        QueryWrapper<WheatCornGrowthYear> wrapper = new QueryWrapper<>();
        wrapper.eq("市", stationName);
        wrapper.eq("起始年份", "平均");
        wrapper.eq("作物", crop);

        WheatCornGrowthYear wheatCornGrowthYear = wheatCornGrowthYearMapper.selectList(wrapper).get(0);

        String startDate;
        String endDate;
        // 反射获取属性（反射是否存在效率问题呢）
        try {
            Field field = WheatCornGrowthYear.class.getDeclaredField(startPeriod);
            field.setAccessible(true);
            startDate = (String) field.get(wheatCornGrowthYear);
            field = WheatCornGrowthYear.class.getDeclaredField(endPeriod);
            field.setAccessible(true);
            endDate = (String) field.get(wheatCornGrowthYear);
        } catch (Exception e) {
            throw new RuntimeException("数据库对应数据为null");
        }

        // 拼接年返回结果，注意，如果是小麦且结束时间是反青，起身，拨节，孕穗，抽穗，开花，吐丝，乳熟，成熟。year是来一年
        // new String[]{"返青", "起身", "拨节", "孕穗", "抽穗", "开花", "吐丝", "乳熟", "成熟"}
        ArrayList<String> filter = new ArrayList<>(Arrays.asList("返青", "起身", "拨节", "孕穗", "抽穗", "开花", "吐丝", "乳熟", "成熟"));


        startDate = year + "-" + startDate;
        if (crop.equals("小麦") && filter.contains(endPeriod)) {
            int yearInt = Integer.parseInt(year);
            yearInt++;
            year = String.valueOf(yearInt);
            endDate = year + "-" + endDate;
        } else {
            endDate = year + "-" + endDate;
        }

        return new String[]{startDate, endDate};
    }

    @Override
    public String[] getTimeRangeImprove(String stationName, String year, String crop, String startPeriod, String endPeriod) {

        Map<String, String> range = wheatCornGrowthYearMapper.getTimeRangeImprove(stationName, crop, startPeriod, endPeriod);
        String startDate = range.get(startPeriod);
        String endDate = range.get(endPeriod);

        // 拼接年返回结果，注意，如果是小麦且结束时间是反青，起身，拨节，孕穗，抽穗，开花，吐丝，乳熟，成熟。year是来一年
        // new String[]{"返青", "起身", "拨节", "孕穗", "抽穗", "开花", "吐丝", "乳熟", "成熟"}
        ArrayList<String> filter = new ArrayList<>(Arrays.asList("返青", "起身", "拨节", "孕穗", "抽穗", "开花", "吐丝", "乳熟", "成熟"));
        startDate = year + "-" + startDate;
        if (crop.equals("小麦") && filter.contains(endPeriod)) {
            int yearInt = Integer.parseInt(year);
            yearInt++;
            year = String.valueOf(yearInt);
            endDate = year + "-" + endDate;
        } else {
            endDate = year + "-" + endDate;
        }

        return new String[]{startDate, endDate};

    }


}
